package com.lsa.letcode.the2.add2num;

import org.junit.Assert;
import org.junit.Test;

import com.lsa.letcode.common.LetCodeTestBase;
import com.lsa.letcode.common.ListNode;

public class Add2NumTest extends LetCodeTestBase {
	@Test
	public void test1() {
//		Solution s = new Solution();
//		System.out.println(toNum(asList(9,2)));
//		System.out.println(toNum(asList(2)));
//		System.out.println(toNum(asList(0, 0, 1)));
//		System.out.println(s.addTwoNumbers(asList(9,2,9), asList(1,2,1)));
		
		Assert.assertEquals(20, toNum(new Solution().addTwoNumbers(asList(1), asList(9, 1))));
		Assert.assertEquals(100, toNum(new Solution().addTwoNumbers(asList(1, 9), asList(9))));
		Assert.assertEquals(99, toNum(new Solution().addTwoNumbers(asList(1, 9), asList(8))));
		Assert.assertEquals(99, toNum(new Solution().addTwoNumbers(asList(8), asList(1, 9))));
		Assert.assertEquals(3, toNum(new Solution().addTwoNumbers(asList(1), asList(2))));
		test(asList(9,2,9), asList(1,2,1));
	}
	
	private void test(ListNode n1, ListNode n2) {
		Solution s = new Solution();
		
		ListNode res = s.addTwoNumbers(n1, n2);
		Assert.assertEquals(toNum(n1) + toNum(n2), toNum(res));
	}
	
	private int toNum(ListNode n) {
		int mul = 1;
		int res = 0;
		
		while(n != null) {
			res += n.val * mul;
			n = n.next;
			mul *= 10;
		}
		return res;
	}
}
